import 'package:flutter_pickers/time_picker/model/date_mode.dart';
import 'package:flutter_pickers/time_picker/model/pduration.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/billing/api/billingApi.dart';
import 'package:honeypotflutter/public/public_picker.dart';
import 'package:honeypotflutter/public/views/public_refresh.dart';
import 'package:honeypotflutter/publicBussine/bussine_date.dart';

class BillingLogic extends GetxController {
  /// 全部
  final HLRefreshController allRefreshController = HLRefreshController(initialRefresh: true);

  /// 收入
  final HLRefreshController inComeRefreshController = HLRefreshController(initialRefresh: true);

  /// 支出
  final HLRefreshController outRefreshController = HLRefreshController(initialRefresh: true);

  /// 选中日期
  RxString selectDate = RxString("");

  /// 月账单
  RxString monryBilling = RxString("收入:0.0元  支出: 0.00元");

  /// 开始时间
  String startTime = "";

  /// 结束时间
  String endTime = "";

  /*
  * 计算开始和结束时间
  * */
  calculateDate({int? yy, int? mm}) async {
    BussineSelectDateModel date = MGBussineDate.calculateDate(yy: yy, mm: mm);
    startTime = date.startTime;
    endTime = date.endTime;
    selectDate.value = date.currentTime;
    requestMonthInfo();
  }

  /*
  * 获取月信息
  * */
  requestMonthInfo() async {
    BillingApi.billingSummation(
      startTime: startTime,
      endTime: endTime,
    ).then((value) {
      monryBilling.value = "收入:${value.totalIncome?.toStringAsFixed(2)}元 支出:${value.totalExpenditure?.toStringAsFixed(2)}元";
    });
  }

  /*
  * 选择时间
  * */
  selectDatePickView() {
    HLPicker.showDatePicker(
      mode: DateMode.YM,
      maxDate: PDuration.now(),
      onConfirm: (PDuration res) async {
        calculateDate(yy: res.year, mm: res.month);
        allRefreshController.requestRefresh();
        inComeRefreshController.requestRefresh();
        outRefreshController.requestRefresh();
      },
    );
  }
}
